package com.tonglin.eduservice.controller;


import com.tonglin.commonutils.R;
import com.tonglin.eduservice.entity.EduChapter;
import com.tonglin.eduservice.entity.charter.ChapterVo;
import com.tonglin.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author tonglin
 * @since 2021-05-10
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

     @Autowired
     private EduChapterService chapterService;
    //课程大纲列表
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){

        List<ChapterVo> list = chapterService.getChapterVideoCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }
//-------------------
    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }
//----------------
    //根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        System.out.println("来看看有没有打通呢---------------");
        chapterService.updateById(eduChapter);
        return R.ok();
    }
    //删除的方法
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
      boolean flag = chapterService.deleteChapter(chapterId);
      if (flag){
          return R.ok();
      }else {
          return R.error();
      }

    }

}

